package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.DeptSearchContent;
import com.isunland.managesystem.entity.EnterpriseList;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

public class SearchDepatrtCollectFragment extends Fragment implements OnClickListener {
  protected static final String EXTRA_VLAUE =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_VLAUE";

  protected static final String EXTRA_STARTDATE =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_STARTDATE";
  protected static final String EXTRA_ENDDATE =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_ENDDATE";
  protected static final String EXTRA_DEPTNAME =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_DEPTNAME";
  protected static final String EXTRA_DEPTCODE =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_DEPTCODE";

  protected static final String EXTRA_CERTIFICATE_NAMES =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_CERTIFICATE_NAMES";
  protected static final String EXTRA_CERTIFICATE_CODES =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_CERTIFICATE_CODES";

  private static final int DEPARTMENT = 0;
  private static final int STARTDATE = 1;
  private static final int ENDDATE = 2;
  private static final int TYPE = 3;
  private static final int CONSTRACT = 4;
  private static final int CUSTOMER = 5;

  private String appDeptCode = "";//部门编码
  private String appDeptName = "";//部门名称
  private String startDate = "";//开始时间
  private String endDate = "";//结束时间

  protected String contractId;//合同id
  protected String contractName;//合同名称
  protected String mContractKind;//合同名称
  protected String mContractCode;//合同名称

  protected String customerName = "";//客户名字
  protected String customerId = "";//客户id

  protected String certificateCodes = "";//单据类型编码codes
  protected String certificateNames = "";//单据类型names

  private TextView tvDepartment;
  private TextView tvStartDate;
  private TextView tvEndDate;
  private TextView tvType;
  private TextView tvConstract;
  private TextView tvCustomer;

  private ImageView ibDepartment;
  private ImageView ibStartDate;
  private ImageView ibEndDate;
  private ImageView ibType;
  private ImageView ibConstract;
  private ImageView ibCustomer;

  private DeptSearchContent content;

  @Override public void onCreate(Bundle savedInstanceState) {

    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);

    appDeptCode = (String) getArguments().getSerializable(EXTRA_DEPTCODE);
    appDeptName = (String) getArguments().getSerializable(EXTRA_DEPTNAME);
    startDate = (String) getArguments().getSerializable(EXTRA_STARTDATE);
    endDate = (String) getArguments().getSerializable(EXTRA_ENDDATE);

    content = (DeptSearchContent) getArguments().getSerializable(EXTRA_VLAUE);
    getData(content);

    super.onCreate(savedInstanceState);
  }

  private void getData(DeptSearchContent content) {
    appDeptCode = content.getAppDeptCode();
    appDeptName = content.getAppDeptName();//部门名称
    startDate = content.getStartDate();//开始时间
    endDate = content.getEndDate();//结束时间

    contractId = content.getContractId();//合同id
    contractName = content.getContractName();//合同名称
    customerName = content.getCustomerName();//客户名字
    customerId = content.getCustomerId();//客户id

    certificateCodes = content.getCertificateCodes();//单据类型编码codes
    certificateNames = content.getCertificateNames();//单据类型names
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        vollelPostConfirm();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //回传 所选值
  private void vollelPostConfirm() {
    Intent backIntent = new Intent();
    DeptSearchContent content =
        new DeptSearchContent(appDeptCode, appDeptName, startDate, endDate, certificateCodes,
            certificateNames, contractId, contractName, customerId, customerName);
    backIntent.putExtra(DepartmentCollectListFragment.EXTRA_VLAUE, content);
    getActivity().setResult(Activity.RESULT_OK, backIntent);
    getActivity().finish();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_search_department, container, false);
    findView(v);
    return v;
  }

  private void findView(View v) {
    tvDepartment = (TextView) v.findViewById(R.id.tv_department);
    tvStartDate = (TextView) v.findViewById(R.id.tv_start_date);
    tvEndDate = (TextView) v.findViewById(R.id.tv_end_date);
    tvConstract = (TextView) v.findViewById(R.id.tv_constract);
    tvCustomer = (TextView) v.findViewById(R.id.tv_customer);
    tvType = (TextView) v.findViewById(R.id.tv_type);

    ibDepartment = (ImageView) v.findViewById(R.id.ib_department);
    ibStartDate = (ImageView) v.findViewById(R.id.ib_start_date);
    ibEndDate = (ImageView) v.findViewById(R.id.ib_end_date);
    ibType = (ImageView) v.findViewById(R.id.ib_type);
    ibConstract = (ImageView) v.findViewById(R.id.ib_constract);
    ibCustomer = (ImageView) v.findViewById(R.id.ib_customer);

    ibDepartment.setOnClickListener(this);
    ibStartDate.setOnClickListener(this);
    ibEndDate.setOnClickListener(this);
    ibType.setOnClickListener(this);
    ibConstract.setOnClickListener(this);
    ibCustomer.setOnClickListener(this);
    //初始化数据
    tvDepartment.setText(appDeptName);
    tvStartDate.setText(startDate);
    tvEndDate.setText(endDate);
    tvType.setText(certificateNames);
    tvConstract.setText(contractName);
    tvCustomer.setText(customerName);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ib_department:
        showDialog(DEPARTMENT);
        break;
      case R.id.ib_start_date:
        showDialog(STARTDATE);
        break;
      case R.id.ib_end_date:
        showDialog(ENDDATE);
        break;
      case R.id.ib_type:
        //			showDialog(TYPE);
        //收入支出类型，可多选
        Intent i = new Intent(getActivity(), TypeListActivity.class);
        startActivityForResult(i, TYPE);
        break;
      case R.id.ib_constract:
        showDialog(CONSTRACT);
        break;
      case R.id.ib_customer:
        //			showDialog(CUSTOMER);
        Intent customerIntent = new Intent(getActivity(), EnterprisePagerActivity.class);
        startActivityForResult(customerIntent, CUSTOMER);
        break;

      default:
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {
      //部门
      case DEPARTMENT:
        dialog = new DeptChooseDialogFragment();
        break;
      //填写开始时间
      case STARTDATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(null, R.string.write_start);
        break;
      //填写结束时间
      case ENDDATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(null, R.string.write_end);
        //只显示 "年月" 的时间控件
        //			dialog = BaseMonthTimeDialogFragment.newInstance(null, R.string.write_end);
        break;
      //合同
      case CONSTRACT:
        dialog = new NewContractDialogFragment();
        break;
      //客户
      case CUSTOMER:
        dialog = new CustomerNameDialogFragment();
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(SearchDepatrtCollectFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {

      return;
    }
    //部门
    if (requestCode == DEPARTMENT) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      appDeptName = customer.getName();
      appDeptCode = customer.getId();
      tvDepartment.setText(appDeptName);//部门
    }

    if (requestCode == STARTDATE) {
      Date mdate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      startDate = MyDateUtil.dateToString(mdate, MyDateUtil.TYPE_DATE);
      tvStartDate.setText(startDate);
      return;
    }
    if (requestCode == ENDDATE) {
      Date startDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      endDate = MyDateUtil.dateToString(startDate, MyDateUtil.TYPE_DATE);
      tvEndDate.setText(endDate);
      return;
    }

    if (requestCode == TYPE) {

      certificateCodes = data.getStringExtra(EXTRA_CERTIFICATE_CODES);
      certificateNames = data.getStringExtra(EXTRA_CERTIFICATE_NAMES);

      tvType.setText(certificateNames);
    }

    if (requestCode == CONSTRACT) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      contractName = customer.getName();
      contractId = customer.getId();
      mContractKind = customer.getJobNo();//合同类别
      mContractCode = customer.getMemberCode();//合同编码
      tvConstract.setText(contractName);//相关合同
    }
    if (requestCode == CUSTOMER) {
      //			CustomerDialog customer = (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      //			customerName=customer.getName();
      //			customerId=customer.getId();
      //			tvCustomer.setText(customerName);

      EnterpriseList item =
          (EnterpriseList) data.getSerializableExtra(EnterpriseListFragment.EXTRA_ENTERPRISE);
      customerName = item.getEnterpriseName();
      customerId = item.getId();

      tvCustomer.setText(customerName);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static SearchDepatrtCollectFragment newInstence(String appDeptCode, String appDeptName,
      String startDate, String endDate) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_DEPTCODE, appDeptCode);
    bundle.putSerializable(EXTRA_DEPTNAME, appDeptName);
    bundle.putSerializable(EXTRA_STARTDATE, startDate);
    bundle.putSerializable(EXTRA_ENDDATE, endDate);
    SearchDepatrtCollectFragment mfragment = new SearchDepatrtCollectFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }

  public static SearchDepatrtCollectFragment newInstence(DeptSearchContent content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VLAUE, content);
    SearchDepatrtCollectFragment mfragment = new SearchDepatrtCollectFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
